package cn.schoolwow.workflow.test.module.oa.common.flow.common;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.deploy.*;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class ConvertWorkFlowDeployJumpCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setCurrentDeployNodeType(flowContext);
        setNextDeployNodeType(flowContext);
        addJump(flowContext);
    }

    @Override
    public String name() {
        return "转换并添加节点跳转";
    }

    private void setCurrentDeployNodeType(FlowContext flowContext){
        if(flowContext.containKey("currentNode")){
            WorkFlowDeployNode currentNode = flowContext.checkData("currentNode", WorkFlowDeployNode.class);
            DeployNodeType currentDeployNodeType = DeployNodeType.getDeployNodeType(currentNode.type);
            flowContext.putCurrentFlowData("currentDeployNodeType", currentDeployNodeType);
            flowContext.putCurrentFlowData("current", currentNode.name);
        }else{
            JSONObject currentChildNode = flowContext.getData("currentChildNode", JSONObject.class);
            flowContext.startFlow(new ConvertAndSetDeployNodeTypeCommonFlow())
                    .putCurrentCompositeFlowData("childNode", currentChildNode)
                    .execute();
            DeployNodeType currentDeployNodeType = flowContext.checkInstanceData(DeployNodeType.class);
            flowContext.putCurrentFlowData("currentDeployNodeType", currentDeployNodeType);

            flowContext.startFlow(new SetConvertWorkFlowNodeNameCommonFlow())
                    .putCurrentCompositeFlowData("childNode", currentChildNode)
                    .execute();
            String nodeName = flowContext.checkData("nodeName", String.class);
            flowContext.putCurrentFlowData("current", nodeName);
        }
    }

    private void setNextDeployNodeType(FlowContext flowContext){
        if(flowContext.containKey("nextNode")){
            WorkFlowDeployNode nextNode = flowContext.checkData("nextNode", WorkFlowDeployNode.class);
            DeployNodeType nextDeployNodeType = DeployNodeType.getDeployNodeType(nextNode.type);
            flowContext.putCurrentFlowData("nextDeployNodeType", nextDeployNodeType);
            flowContext.putCurrentFlowData("next", nextNode.name);
        }else{
            JSONObject nextChildNode = flowContext.getData("nextChildNode", JSONObject.class);
            flowContext.startFlow(new ConvertAndSetDeployNodeTypeCommonFlow())
                    .putCurrentCompositeFlowData("childNode", nextChildNode)
                    .execute();
            DeployNodeType nextDeployNodeType = flowContext.checkInstanceData(DeployNodeType.class);
            flowContext.putCurrentFlowData("nextDeployNodeType", nextDeployNodeType);

            flowContext.startFlow(new SetConvertWorkFlowNodeNameCommonFlow())
                    .putCurrentCompositeFlowData("childNode", nextChildNode)
                    .execute();
            String nodeName = flowContext.checkData("nodeName", String.class);
            flowContext.putCurrentFlowData("next", nodeName);
        }
    }

    private void addJump(FlowContext flowContext){
        DeployNodeType currentDeployNodeType = flowContext.checkData("currentDeployNodeType", DeployNodeType.class);
        DeployNodeType nextDeployNodeType = flowContext.checkData("nextDeployNodeType", DeployNodeType.class);
        String current = flowContext.checkData("current", String.class);
        String next = flowContext.checkData("next", String.class);
        List<List<WorkFlowDeployJumpFilterCondition>> filterCondition = flowContext.getData("filterCondition", List.class);
        Integer priorityLevel = flowContext.getData("priorityLevel", Integer.class);
        String jumpName = flowContext.getData("jumpName", String.class);
        WorkFlowDeployFileContext workFlowDeployFileContext = flowContext.checkInstanceData(WorkFlowDeployFileContext.class);

        WorkFlowDeployJump workFlowDeployJump = new WorkFlowDeployJump();
        workFlowDeployJump.current = current;
        workFlowDeployJump.next = next;
        workFlowDeployJump.jumpName = jumpName;
        switch (currentDeployNodeType){
            case ConditionNode:{
                flowContext.brokenCurrentFlow("跳过条件节点");
            }break;
            case ExclusiveGateway:{
                if(!current.endsWith("开始")){
                    workFlowDeployJump.current = current+"结束";
                }
            }break;
        }
        switch (nextDeployNodeType){
            case ExclusiveGateway:{
                if(!next.endsWith("结束")){
                    workFlowDeployJump.next = next+"开始";
                }
            }break;
        }
        workFlowDeployJump.filterCondition = filterCondition;
        workFlowDeployJump.priorityLevel = priorityLevel;
        workFlowDeployFileContext.jumpArray.add(workFlowDeployJump);
    }

}
